
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newUser:true,
    tabbarHeight:"",
    statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
    // 导航栏高度
    navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
    // 胶囊按钮高度
    menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
    // 导航栏和状态栏高度
    navigationBarAndStatusBarHeight:
      wx.getStorageSync('statusBarHeight') +
      wx.getStorageSync('navigationBarHeight') +
      'px',
    score:0,
    showUpload:false,
    list: [
      {
        "text": "首页",
        
        "iconPath": "/images/home.png",
        "selectedIconPath": "/images/home_active.png"
      },
      {
        "text": "文库",
        
        "iconPath": "/images/library.png",
        "selectedIconPath": "/images/library_active.png"
      },
      {
        "text": "社区",
        
        "iconPath": "/images/community.png",
        "selectedIconPath": "/images/community_active.png"
      },
      {
        "text": "我的",
        
        "iconPath": "/images/user.png",
        "selectedIconPath": "/images/user_active.png"
      }
    ]
    
  },
  goToCollect:function(){
    wx.navigateTo({
      url: '/pages/collect/collect',
    })
  },
  goToErrors:function(){
    wx.navigateTo({
      url: '/pages/errors/errors',
    })
  },
  
  goToAbout:function(){
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },
  goToUpload:function () {
    wx.navigateTo({
      url: '/pages/upload_file/upload_file',
    })
  },
  goToFeedback:function(){
    wx.navigateTo({
      url: '/pages/feedback/feedback',
    })
  },
  goToRecord:function(){
    wx.navigateTo({
      url: '/pages/practice_record/practice_record',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let index = options.pageIndex
    this.setData({pageIndex : index})
    let app = getApp()
    let newUser = app.globalData.newUser
    console.log(newUser)
    this.setData({
      score:app.globalData.userInfo.score,
      newUser:newUser,
    })
    let setting = app.globalData.setting
    this.setData({
      showUpload : setting.showUpload == "0" ? false : true
    })
    
  },

  onReady: function () {
    //计算tabbar高度
    let tabbarHeight = wx.getStorageSync('tabBarHeight')
    if(tabbarHeight == 0){
      console.log(tabbarHeight)
      tabbarHeight = 82
    }
    this.setData({tabbarHeight: tabbarHeight + "px"})
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
    let app = getApp()
    this.setData({
      score:app.globalData.userInfo.score,
    })
  },
  onGotUserInfo: function (e) {
    console.log(e)
    if(e.detail.errMsg == "getUserInfo:fail auth deny"){
      wx.showToast({
        title: '登录失败',
        icon: 'error',
        duration: 1000
      })   
    }else{
      let app = getApp()
      app.globalData.userInfo.nickName = e.detail.userInfo.nickName
      app.globalData.userInfo.avatarUrl = e.detail.userInfo.avatarUrl
      app.globalData.userInfo.country = e.detail.userInfo.country
      app.globalData.userInfo.province = e.detail.userInfo.province
      app.globalData.userInfo.city = e.detail.userInfo.city
      app.globalData.userInfo.gender = e.detail.userInfo.gender
      
      this.saveUser()
    }
  },
  processUserInfo:function(){
    let app = getApp()
    let gender = "man"
    if(app.globalData.userInfo.gender == 0){
      gender = "woman"
    }
    let address = app.globalData.userInfo.country
    if(address == "China" || address == "中国"){
      address = ""
    }
    if(app.globalData.userInfo.province != ""){
      address = address + " " + app.globalData.userInfo.province
    }
    if(app.globalData.userInfo.city != ""){
      address = address + " " + app.globalData.userInfo.city
    }
    return {"gender":gender,"address":address}
  },
  saveUser: function(){
    let app = getApp()
    let _userInfo = this.processUserInfo()
    wx.request({
      url: app.globalData.url + '/user/save',
      method: "POST",
      data: {
        nickname: app.globalData.userInfo.nickName,
        avatar: app.globalData.userInfo.avatarUrl,
        gender: _userInfo.gender,
        address: _userInfo.address,
        id: app.globalData.userInfo.openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success :res=> {
        app.globalData.newUser = false
        this.setData({
          newUser:false,
        })
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1000
        })    
      }
    })
  },
  onHide: function () {

  },
  onUnload: function () {

  },

  onPullDownRefresh: function () {

  },

  onReachBottom: function () {

  },

  onShareAppMessage: function () {

  }
})